package main

import (
  "sdl"
  "sdl/ttf"
  "log"
)

type Button struct {
  Base
  label    string
  pressed  bool
  callback func (Window, int, int)
}

func NewButton(x, y, w, h int, label string, cb func(Window, int, int)) *Button {
  button := new(Button)
  button.x = x
  button.y = y
  button.w = w
  button.h = h
  button.label = label
  button.callback = cb
  return button
}

func (d *Button) print() { 
  log.Stderr(d.x, d.y) 
}
func (d *Button) pointerPressed(x int, y int) { 
  log.Stderr("Button pressed", x, y) 
  d.pressed = true
}

func (d *Button) pointerReleased(x int, y int) { 
  log.Stderr("Button released", x, y) 
  d.pressed = false
  if d.callback != nil {
    d.callback(d, x, y)
  }
}

func (d *Button) draw(surface *sdl.Surface) {
  var color uint32 = 10000
  
  if (Window)(d) == GetActiveWindow() {
    surface.FillRect(&sdl.Rect{int16(d.x) - 1, int16(d.y) - 1, uint16(d.w) + 2, uint16(d.h) + 2}, 0xff0000)
  }
  
  if d.pressed {
    color = 100000
  }
  surface.FillRect(&sdl.Rect{int16(d.x), int16(d.y), uint16(d.w), uint16(d.h)}, color);
  
  text := ttf.RenderText_Blended(GetFont(), d.label, sdl.Color{255, 255, 255, 0})
  center := (int16(d.w) - int16(text.W) - 5) / 2
  surface.Blit(&sdl.Rect{int16(d.x) + 5 + center, int16(d.y + 5), 0, 0}, text, nil)
  text.Free()
  
  //log.Stderrf("Drawing window %p", d)
}
